package pubsub;

import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.jersey.JerseyBroadcaster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * A example of how to extends/replace {@link org.atmosphere.cpr.Broadcaster} by defining the
 * class value in META-INF/atmosphere.xml or by adding defining the org.atmosphere.cpr.broadcasterClass
 * init-param in web.xml.
 *
 * @author Jeanfrancois Arcand
 */
public class PubSubBroadcaster extends JerseyBroadcaster {

    private static final Logger logger = LoggerFactory.getLogger(PubSubBroadcaster.class);

    private final AtomicBoolean isSet = new AtomicBoolean(false);

    @Override
    protected void invokeOnStateChange(final AtmosphereResource r, final AtmosphereResourceEvent e) {
        if (!isSet.getAndSet(true)) {
            logger.info("This is just an example that demonstrate " +
                    "how a Broadcaster can be customized using atmosphere.xml or by " +
                    "defining it inside web.xml");
        }

        super.invokeOnStateChange(r, e);
    }
}